//
//  DataFrameParam.m
//  ChampoinPeso
//
//  Created by mac on 2025/2/25.
//

#import "DataFrameParam.h"
#import <AppTrackingTransparency/AppTrackingTransparency.h>
#import <AdSupport/AdSupport.h>
#import <sys/utsname.h>

@implementation DataFrameParam

+ (NSString *)frameMoverInfoPart {
    NSString *lived = [self lived];
    NSString *nervous = [self nervous];
    NSString *stewardess = [self stewardess];
    NSString *armyoure = [self armyoure];
    NSString *stuck = [self stuck];
    NSString *even = [self even];
    NSString *nutshell =[self nutshell];
    
    NSString *frameMover = [NSString stringWithFormat:@"?lived=%@&nervous=%@&stewardess=%@&armyoure=%@&stuck=%@&even=%@&nutshell=%@",lived,nervous,stewardess,armyoure,stuck,even,nutshell];
    NSString *backMover = [frameMover stringByReplacingOccurrencesOfString:@" " withString:@""];
    backMover = [backMover stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet characterSetWithCharactersInString:@"%^{}\"[]|\\<> "].invertedSet];
    return backMover;
}


#pragma mark -URL-
+ (NSString *)lived {
    return [(__bridge NSDictionary *)CFBundleGetInfoDictionary(CFBundleGetMainBundle())objectForKey:@"CFBundleShortVersionString"];
}


+ (NSDictionary *)deviceType {
    return
    @{
        @"iPhone1,1" : @"iPhone",
        @"iPhone1,2" : @"iPhone3G",
        @"iPhone2,1" : @"iPhone3GS",
        @"iPhone3,1" : @"iPhone4",
        @"iPhone3,2" : @"iPhone4",
        @"iPhone3,3" : @"iPhone4",
        @"iPhone4,1" : @"iPhone4S",
        @"iPhone5,1" : @"iPhone5",
        @"iPhone5,2" : @"iPhone5",
        @"iPhone5,3" : @"iPhone5c",
        @"iPhone5,4" : @"iPhone5c",
        @"iPhone6,1" : @"iPhone5s",
        @"iPhone6,2" : @"iPhone5s",
        @"iPhone7,2" : @"iPhone6",
        @"iPhone7,1" : @"iPhone6Plus",
        @"iPhone8,1" : @"iPhone6s",
        @"iPhone8,2" : @"iPhone6sPlus",
        @"iPhone8,4" : @"iPhoneSE1st",
        @"iPhone9,1" : @"iPhone7",
        @"iPhone9,3" : @"iPhone7",
        @"iPhone9,2" : @"iPhone7Plus",
        @"iPhone9,4" : @"iPhone7Plus",
        @"iPhone10,1" : @"iPhone8",
        @"iPhone10,4" : @"iPhone8",
        @"iPhone10,2" : @"iPhone8Plus",
        @"iPhone10,5" : @"iPhone8Plus",
        @"iPhone10,3" : @"iPhoneX",
        @"iPhone10,6" : @"iPhoneX",
        @"iPhone11,8" : @"iPhoneXR",
        @"iPhone11,2" : @"iPhoneXS",
        @"iPhone11,6" : @"iPhoneXSMax",
        @"iPhone11,4" : @"iPhoneXSMax",
        @"iPhone12,1" : @"iPhone11",
        @"iPhone12,3" : @"iPhone11Pro",
        @"iPhone12,5" : @"iPhone11ProMax",
        @"iPhone12,8" : @"iPhoneSE2nd",
        @"iPhone13,1" : @"iPhone12mini",
        @"iPhone13,2" : @"iPhone12",
        @"iPhone13,3" : @"iPhone12Pro",
        @"iPhone13,4" : @"iPhone12ProMax",
        @"iPhone14,4" : @"iPhone13mini",
        @"iPhone14,5" : @"iPhone13",
        @"iPhone14,2" : @"iPhone13Pro",
        @"iPhone14,3" : @"iPhone13ProMax",
        @"iPhone14,6" : @"iPhoneSE3rd",
        @"iPhone14,7" : @"iPhone14",
        @"iPhone14,8" : @"iPhone14Plus",
        @"iPhone15,2" : @"iPhone14Pro",
        @"iPhone15,3" : @"iPhone14ProMax",
        @"iPhone15,4" : @"iPhone15",
        @"iPhone15,5" : @"iPhone15Plus",
        @"iPhone16,1" : @"iPhone15Pro",
        @"iPhone16,2" : @"iPhone15ProMax",
        @"iPhone17,3" : @"iPhone16",
        @"iPhone17,4" : @"iPhone16Plus",
        @"iPhone17,1" : @"iPhone16Pro",
        @"iPhone17,2" : @"iPhone16ProMax",
    };
}

+ (NSString *)nervous {
    
    NSDictionary *deviceTypes = [self deviceType];
    struct utsname systemInfo;
    
    if (uname(&systemInfo) < 0) {
        return @"";
    } else {
        NSString *deviceIdentifer = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
        if([deviceIdentifer containsString:@"iPad"]){
            return @"iPad";
        } else
        if([deviceIdentifer containsString:@"i386"]){
            return @"iPhoneSimulator";
        } else
        if([deviceIdentifer containsString:@"x86_64"]){
            return @"iPhoneSimulator";
        } else {
            NSString *deviceType = [deviceTypes objectForKey:deviceIdentifer];
            return deviceType?:@"";
        }
    }
}

+ (NSString *)office {
    
    
    struct utsname systemInfo;
    
    if (uname(&systemInfo) < 0) {
        return @"";
    } else {
        NSString *deviceIdentifer = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
        if([deviceIdentifer containsString:@"iPad"]){
            return @"iPad";
        } else
        if([deviceIdentifer containsString:@"i386"]){
            return @"iPhoneSimulator";
        } else
        if([deviceIdentifer containsString:@"x86_64"]){
            return @"iPhoneSimulator";
        } else {
            if(deviceIdentifer != nil && ![deviceIdentifer isEqualToString:@""]){
                return deviceIdentifer;
            } else {
                return @"";
            }
        }
    }
}




+ (NSString *)armyoure{
    //idfv
    NSString *armyoure = [self keychainGetDataWithAccountIdentifier:@"idet" andServiceIdentifier:@"armyoure"];
    if(armyoure == nil || [armyoure isEqualToString:@""]){
        armyoure = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
        if(armyoure == nil || [armyoure isEqual:[NSNull null]]){
            armyoure = @"";
        }
        [self keychainSaveData:armyoure withAccountIdentifier:@"idet" andServiceIdentifier:@"armyoure"];
    }
    return armyoure;
}



+ (NSString *)stuck {
    return [UIDevice currentDevice].systemVersion;
}

+ (NSString *)stewardess {
    
    NSString *stewardess = [[NSUserDefaults standardUserDefaults] objectForKey:@"stewardess"];
    if(stewardess != nil){
        return stewardess;
    } else {
        return @"";
    }
}

+ (NSString *)even{
    //idfa
    return [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
}

+ (NSString *)nutshell {
    return @"1";
}

#pragma mark -long time-
+ (id)keychainGetDataWithAccountIdentifier:(NSString *)accountId andServiceIdentifier:(NSString *)serviceId{
    id idObject = nil ;
    NSMutableDictionary * readQueryDic = [self keychainDicWithAccountId:accountId andServiceId:serviceId];
    [readQueryDic setObject:(id)kCFBooleanTrue forKey:(id)kSecReturnData];
    [readQueryDic setObject:(id)kSecMatchLimitOne forKey:(id)kSecMatchLimit];
    CFDataRef keyChainData = nil;
    if (SecItemCopyMatching((CFDictionaryRef)readQueryDic , (CFTypeRef *)&keyChainData) == noErr){
        @try {
            idObject = [NSKeyedUnarchiver unarchiveObjectWithData:(__bridge NSData *)(keyChainData)];
        } @catch (NSException * exception){
            NSLog(@"Unarchive of search data where %@ failed of %@ ",serviceId,exception);
        }
    }
    if (keyChainData) {
        CFRelease(keyChainData);
    }
    readQueryDic = nil;
    return idObject ;
}

+ (NSMutableDictionary *)keychainDicWithAccountId:(NSString *)accountId andServiceId:(NSString *)serviceId{
    NSString *classKey = (__bridge NSString *)kSecClass;
    NSString *classValue = (__bridge NSString *)kSecClassGenericPassword;
    NSString *accessibleKey = (__bridge NSString *)kSecAttrAccessible;
    NSString *accessibleValue = (__bridge NSString *)kSecAttrAccessibleAlways;
    NSString *accountKey = (__bridge NSString *)kSecAttrAccount;
    NSString *accountValue = accountId;
    NSString *serviceKey = (__bridge NSString *)kSecAttrService;
    NSString *serviceValue = serviceId;
    NSDictionary *keychainItems = @{classKey      : classValue,
                                    accessibleKey : accessibleValue,
                                    accountKey    : accountValue,
                                    serviceKey    : serviceValue};
    return keychainItems.mutableCopy;
}

+ (BOOL)keychainSaveData:(id)aData withAccountIdentifier:(NSString *)accountId andServiceIdentifier:(NSString *)serviceId{
    NSMutableDictionary * saveQueryDic = [self keychainDicWithAccountId:accountId andServiceId:serviceId];
    SecItemDelete((CFDictionaryRef)saveQueryDic);
    [saveQueryDic setObject:[NSKeyedArchiver archivedDataWithRootObject:aData] forKey:(id)kSecValueData];
    OSStatus saveState = SecItemAdd((CFDictionaryRef)saveQueryDic, nil);
    saveQueryDic = nil ;
    if (saveState == errSecSuccess) {
        return YES;
    }
    return NO;
}

#pragma mark -Others-
@end
